﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Cofe.Core.Session;

namespace Cofe.Web.Session
{
    public class AppData_SuspensionManager : ISuspensionManager
    {

         public AppData_SuspensionManager()
        {

        }

        public AppData_SuspensionManager(string filename)
        {            
            _filename = filename;
        }
        
        private Dictionary<string, object> sessionState_ = new Dictionary<string, object>();
        private List<Type> knownTypes_ = new List<Type>();
        private string _filename = "_cofe.xml";

        public Dictionary<string, object> SessionState
        {
            get { return sessionState_; }
        }

        public List<Type> KnownTypes
        {
            get { return knownTypes_; }
            set { knownTypes_ = value; }
        }

        public Task SaveAsync()
        {
            return Task.Run(() =>
                {
                    string path = System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data", _filename);
                    
                    using (var outStream = new FileStream(path, FileMode.Create))
                    {
                        DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_);
                        serializer.WriteObject(outStream, sessionState_);
                        outStream.Flush();
                    }
                });
        }

        // Restore the saved sesison state
        public Task RestoreAsync()
        {
            return Task.Run(() =>
                {
                    // Get the input stream for the SessionState file.
                    try
                    {
                        string path = System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data", _filename);
                        using (var inStream = new FileStream(path, FileMode.OpenOrCreate))
                        {
                            //using (var sr = new StreamReader(inStream))
                            //    Console.WriteLine(sr.ReadToEnd());
                            //inStream.Seek(0, SeekOrigin.Begin);                            
                            DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_);
                            sessionState_ = (Dictionary<string, object>)serializer.ReadObject(inStream);
                        }
                    }
                    catch (Exception)
                    {
                        //Console.WriteLine(ex.Message);
                        // Restoring state is best-effort.  If it fails, the app will just come up with a new session.
                    }
                });
        }
    }
}
